package com.ebt.m.commons.mvp;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;

import com.ebt.m.commons.utils.ActivityUtils;
import com.ebt.m.commons.widgets.BaseRxActivity;

import butterknife.ButterKnife;

/**
 * Created by archer.qi on 2017/1/24.
 */
public abstract class MvpBaseActivity<P extends IPresenter> extends BaseRxActivity {
    protected P mPresenter;
    protected View mContentView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContentView = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(setContentResource(), null);
        setContentView(mContentView);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        ButterKnife.bind(this, mContentView);
        mPresenter = createPresenter();
        mPresenter.start();
        onActivityCreate();
    }

    /**
     * create presenter implements IPresenter
     *
     * @return
     */
    protected abstract P createPresenter();

    /**
     * set resourceId of contentView
     *
     * @return
     */
    protected abstract int setContentResource();

    /**
     * do activity-init
     *
     * @return
     */
    protected abstract void onActivityCreate();

    /**
     * do activity-destroy
     */
    protected abstract void onActivityDestroy();

    /**
     * default intent to activity
     *
     * @param cls
     */
    protected void launch(Class<?> cls) {
        ActivityUtils.launchActivity(this, cls);
    }

    /**
     * intent to activity with data
     *
     * @param cls
     * @param data
     */
    protected void launchWithData(Class<?> cls, Bundle data) {
        ActivityUtils.launchActivity(this, cls, data);
    }

    /**
     * 检查空字符串
     *
     * @param s
     * @return
     */
    protected String checkNull(String s) {
        return s == null ? "" : s;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        onActivityDestroy();
//        if (mPresenter != null) {
//            mPresenter.onDestroy();
//        }
    }

}
